#pragma once
#include "Manager.h"
#include <iostream>
#include "msclr\marshal_cppstd.h"
#include <string>
//@author A0066758M
namespace CS2103v01 {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	/// <summary>
	/// Summary for AddAccount
	/// </summary>
	public ref class AddAccount : public System::Windows::Forms::Form
	{
	public:
		AddAccount(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			TM = Manager::get();
			label3->Text = "";
			label4->Text = "";
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~AddAccount()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::Button^  button1;
	protected: 
	private: System::Windows::Forms::Label^  label1;
	private: System::Windows::Forms::Label^  label2;
	private: System::Windows::Forms::TextBox^  textBox1;
	private: System::Windows::Forms::TextBox^  textBox2;

	private:
		/// <summary>
		/// Required designer variable.
		Manager* TM;
	private: System::Windows::Forms::Label^  label3;
	private: System::Windows::Forms::Label^  label4;
			 /// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->textBox1 = (gcnew System::Windows::Forms::TextBox());
			this->textBox2 = (gcnew System::Windows::Forms::TextBox());
			this->label3 = (gcnew System::Windows::Forms::Label());
			this->label4 = (gcnew System::Windows::Forms::Label());
			this->SuspendLayout();
			// 
			// button1
			// 
			this->button1->Font = (gcnew System::Drawing::Font(L"Microsoft YaHei", 9, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(134)));
			this->button1->Location = System::Drawing::Point(250, 168);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(75, 23);
			this->button1->TabIndex = 0;
			this->button1->Text = L"OK";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &AddAccount::button1_Click);
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Font = (gcnew System::Drawing::Font(L"Microsoft YaHei", 9, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(134)));
			this->label1->Location = System::Drawing::Point(69, 70);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(123, 17);
			this->label1->TabIndex = 1;
			this->label1->Text = L"New Account Name";
			// 
			// label2
			// 
			this->label2->AutoSize = true;
			this->label2->Font = (gcnew System::Drawing::Font(L"Microsoft YaHei", 9, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(134)));
			this->label2->Location = System::Drawing::Point(69, 111);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(88, 17);
			this->label2->TabIndex = 2;
			this->label2->Text = L"Initial Deposit";
			// 
			// textBox1
			// 
			this->textBox1->Location = System::Drawing::Point(215, 68);
			this->textBox1->Name = L"textBox1";
			this->textBox1->Size = System::Drawing::Size(100, 21);
			this->textBox1->TabIndex = 3;
			// 
			// textBox2
			// 
			this->textBox2->Location = System::Drawing::Point(215, 109);
			this->textBox2->Name = L"textBox2";
			this->textBox2->Size = System::Drawing::Size(100, 21);
			this->textBox2->TabIndex = 4;
			// 
			// label3
			// 
			this->label3->AutoSize = true;
			this->label3->Font = (gcnew System::Drawing::Font(L"Microsoft YaHei", 8.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(134)));
			this->label3->ForeColor = System::Drawing::Color::Red;
			this->label3->Location = System::Drawing::Point(213, 92);
			this->label3->Name = L"label3";
			this->label3->Size = System::Drawing::Size(39, 16);
			this->label3->TabIndex = 5;
			this->label3->Text = L"label3";
			// 
			// label4
			// 
			this->label4->AutoSize = true;
			this->label4->Font = (gcnew System::Drawing::Font(L"Microsoft YaHei", 8.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(134)));
			this->label4->ForeColor = System::Drawing::Color::Red;
			this->label4->Location = System::Drawing::Point(213, 133);
			this->label4->Name = L"label4";
			this->label4->Size = System::Drawing::Size(39, 16);
			this->label4->TabIndex = 6;
			this->label4->Text = L"label4";
			// 
			// AddAccount
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(384, 211);
			this->Controls->Add(this->label4);
			this->Controls->Add(this->label3);
			this->Controls->Add(this->textBox2);
			this->Controls->Add(this->textBox1);
			this->Controls->Add(this->label2);
			this->Controls->Add(this->label1);
			this->Controls->Add(this->button1);
			this->Name = L"AddAccount";
			this->Text = L"Add Account";
			this->Load += gcnew System::EventHandler(this, &AddAccount::AddAccount_Load);
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	private: System::Void AddAccount_Load(System::Object^  sender, System::EventArgs^  e) {
				 label3->Text = "";
				 label4->Text = "";
			 }
	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
				 if(!checkField(textBox1, label3, textBox2, label4))
					 return;

				 std::string NEW="";
				 double DEP=0;

				 NEW = msclr::interop::marshal_as<std::string>(textBox1->Text);
				 DEP = System::Convert::ToDouble(textBox2->Text);

				 if(DEP<0)
				 {
					 label4->Text="must be positve";
					 return;
				 }
				 if(NEW=="delete")
				 {
					 label3->Text="\"delete\" are reserved,\nplease choose another name.";
					 return;
				 }
				 if(TM->addAccount(NEW, DEP))
					 this->Close();
				 else
					 label3->Text="Existed";
			 }

	private: bool checkField(TextBox^ acc, Label^ accL, TextBox^ dep, Label^ depL)
			 {
				 accL->Text="";
				 depL->Text="";

				 bool miss=false;
				 bool format=true;

				 //check empty field
				 if(acc->Text == "")
				 {
					 accL->Text="missing";
					 miss = true;
				 }
				 if(dep->Text == "")
				 {
					 depL->Text="missing";
					 miss = true;
				 }
				 if(miss)
					 return false;

				 //check format
				 if(!(TM->checkDouble(msclr::interop::marshal_as<std::string>(dep->Text))))
				 {
					 depL->Text="Not numeric";
					 format = false;
				 }
				 else if(!TM->checkDP(msclr::interop::marshal_as<std::string>(dep->Text)))
				 {
					 depL->Text="exceeded precision";
					 format = false;
				 }
				 return format;
			 }

};
}
